<?php
class ApplicantsController extends AppController {

	var $name = 'Applicants';
	var $helpers = array('Html', 'Form');

    function new_reply()
    {
        $data = $this->Applicant->find('all',array('conditions'=>array('Applicant.jobseeker_id'=>$this->Session->read('User.id'),'Applicant.status'=>'1','Applicant.viewed'=>'0')));
        if(!empty($data))
        { return true; }
        else
        { return false; }
    }    
    
    function alert()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',44);
            $this->Job->recursive=-1;            
            $query = "SELECT DISTINCT(jobs.id) FROM `jobs`,`applicants` WHERE jobs.id = applicants.job_id AND 
                      jobs.employer_id = ".$this->Session->read('User.id');
//                      jobs.employer_id = ".$this->Session->read('User.id')." AND applicants.status = 0 ";
            
            $query = "SELECT jobs.*, (SELECT count(*) FROM `applicants` WHERE applicants.job_id = jobs.id ) as total FROM `jobs` WHERE jobs.id IN (".$query.") ORDER BY jobs.created DESC";
            
            
            $jobs = $this->Applicant->query($query);   
            //var_dump($jobs);     
            
            $this->set('jobs',$jobs);                          


     
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }        
    }
    
    function apply( $id = null )
    {
        if($this->Session->read('User.type')=='jobseeker')    
        {
            $this->set('choose1',35); 
            if(empty($this->data))
            {
                $this->Applicant->Job->recursive=0;
                $data = $this->Applicant->Job->find('all',array('conditions'=>array('Job.id'=>$id, 'Job.status'=>'1', 'Job.end_date >='=>date("Y-m-d"))));                
                if($data)
                {
                    $this->Applicant->recursive=-1;
                    $data2 = $this->Applicant->find('all',array('conditions'=>array('Applicant.job_id'=>$id,'Applicant.jobseeker_id'=>$this->Session->read('User.id'))));
                    if(empty($data2))
                    {
                        //$jobseeker = $this->Applicant->Jobseeker->find('first',array('conditions'=>array('Jobseeker.id'=>$this->Session->read('User.id'))));
                        //$job = $this->Applicant->Job->find('first',array('conditions'=>array('Job.id'=>$id)));

                        $jobseeker = $this->Applicant->query("SELECT jobseekers.first_name, jobseekers.last_name, jobseekers.city_id, resumes.academic_id, resumes.upload_file FROM `jobseekers`,`resumes` WHERE resumes.jobseeker_id = jobseekers.id AND jobseekers.id=".$this->Session->read('User.id'));
                        $this->set('jobseeker',$jobseeker[0]);   
                        $job = $this->Applicant->query("SELECT jobs.position, jobs.academic_id, jobs.city_id, jobs.created, employers.company_name, employers.email FROM `jobs`,`employers` WHERE employers.id = jobs.employer_id AND jobs.id = ".$id);
                        $this->set('job',$job[0]);                        
                        
                        $this->data['Applicant']['jobseeker_id']=$this->Session->read('User.id');
                        $this->data['Applicant']['job_id']=$id;
                        //$this->data['Applicant']['email']=$job[0]['employers']['email'];
                    }
                    else
                    {
                        $this->Session->setFlash(__('You have applied this job, please re-check your applications', true));
                        $this->redirect(array('controller'=>'applicants', 'action'=>'my_applications'),null, true);
                    }
                }
                else
                {
                    $this->Session->setFlash(__('You have request an invalid link', true));
                    $this->redirect(array('controller'=>'applicants', 'action'=>'my_applications'),null, true);
                }
            }
            else
            {
                $this->Applicant->create();
                $this->Applicant->save($this->data);                
                $this->Session->setFlash(__('The application has been sent, please wait for the confirmation', true));
                $this->redirect(array('controller'=>'applicants', 'action'=>'my_applications'),null, true);
            }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }
    
    }
    
    function apply_ajax ( $id = null )
    {
        if($this->Session->read('User.type')=='jobseeker')    
        {
            $this->layout = 'ajax'; 
            if(empty($this->data))
            {
                $this->Applicant->Job->recursive=0;
                $data = $this->Applicant->Job->find('all',array('conditions'=>array('Job.id'=>$id, 'Job.status'=>'1', 'Job.end_date >='=>date("Y-m-d"))));                
                if($data)
                {
                    $this->Applicant->recursive=-1;
                    $data2 = $this->Applicant->find('all',array('conditions'=>array('Applicant.job_id'=>$id,'Applicant.jobseeker_id'=>$this->Session->read('User.id'))));
                    if(empty($data2))
                    {
                        //$jobseeker = $this->Applicant->Jobseeker->find('first',array('conditions'=>array('Jobseeker.id'=>$this->Session->read('User.id'))));
                        //$job = $this->Applicant->Job->find('first',array('conditions'=>array('Job.id'=>$id)));

                        $jobseeker = $this->Applicant->query("SELECT jobseekers.first_name, jobseekers.last_name, jobseekers.city_id, resumes.academic_id, resumes.upload_file FROM `jobseekers`,`resumes` WHERE resumes.jobseeker_id = jobseekers.id AND jobseekers.id=".$this->Session->read('User.id'));
                        $this->set('jobseeker',$jobseeker[0]);   
                        $job = $this->Applicant->query("SELECT jobs.position, jobs.academic_id, jobs.city_id, jobs.created, employers.company_name, employers.email FROM `jobs`,`employers` WHERE employers.id = jobs.employer_id AND jobs.id = ".$id);
                        $this->set('job',$job[0]);                        
                        
                        $this->data['Applicant']['jobseeker_id']=$this->Session->read('User.id');
                        $this->data['Applicant']['job_id']=$id;
                        //$this->data['Applicant']['email']=$job[0]['employers']['email'];
                        $this->set('params','apply');
                    }
                    else
                    {
                        $this->set('params','kembar');
                    }
                }
                else
                {
                    $this->set('params','invalid');
                }
            }
            else
            {
                $this->Applicant->create();
                $this->Applicant->save($this->data);   
                $this->set('params','berhasil');             
            }
        }
    }

    function my_applications()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',35);
            if(!empty($this->data))
            {
                for($i=0;$i<$this->data['jumlah'];$i++)
                {
                    $temp = 'check'.$i;
                    if( !empty($this->data[$temp]) )
                    {
                        $this->Applicant->del($this->data[$temp]);
                    }               
                }
                $this->Session->setFlash(__('Your application(s) has been withdrawn', true));
            }
            
            
            $query = "SELECT applicants.id, applicants.reply, applicants.viewed, applicants.status, applicants.resume ,applicants.created, jobs.created, jobs.end_date, jobs.id,jobs.position, employers.id,employers.company_name FROM `employers`,`jobs`,`applicants` WHERE jobs.id=applicants.job_id AND jobs.employer_id=employers.id AND applicants.jobseeker_id =".$this->Session->read('User.id')." ORDER BY applicants.created DESC";
            $data = $this->Applicant->query($query);
            $this->set('applicants',$data);
            
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }
          
    }
    
    function view_reply($id)
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            
            $data = $this->Applicant->find('all',array('conditions'=>array('Applicant.id'=>$id,'Applicant.jobseeker_id'=>$this->Session->read('User.id'),'Applicant.status'=>1)));   
            if(!empty($data))
            {
                $this->layout = 'ajax';
                $applicant = $this->Applicant->read(null,$id);
                if($applicant['Applicant']['viewed']==0)
                {
                    $applicant['Applicant']['viewed']=1;
                    $this->Applicant->save($applicant);
                }
                $this->set('applicant',$applicant);   
                $this->set('cities',$this->Applicant->query("SELECT name FROM `cities` WHERE id=".$applicant['Job']['city_id']));
                $this->set('company',$this->Applicant->query("SELECT company_name FROM `employers` WHERE id=".$applicant['Job']['employer_id']));
                             
            }
            else
            {
                $this->Session->setFlash(__('Invalid ID', true));
                $this->redirect(array('controller'=>'applicants', 'action'=>'my_applications'),null, true);
            }       
        }
    }

    function new_applicant($id = null)
    {
        $query = "SELECT * FROM `applicants` WHERE job_id=".$id." AND status=0 AND viewed=0";
        $data = $this->Applicant->query($query);
        //$data = $this->Applicant->find('all',array('conditions'=>array('Applicant.job_id'=>$id,'Applicant.status'=>'0','Applicant.viewed'=>'0')));
        if(!empty($data))
        { $temp = true; }
        else
        { $temp = false; } 
        
        $query = "SELECT count(*) as total FROM `applicants` WHERE job_id=".$id;
        $data = $this->Applicant->query($query);
        $data[0][0]['new']=$temp;
        return $data[0][0];
    }    
    
    function accept( $id = null )
    {
        if($this->Session->read('User.type')=='employer')    
        {
            $this->set('choose1',44);
            if(!empty($this->data))
            {
                //var_dump($_POST); exit;
                $applicants = $this->Applicant->query("SELECT applicants.id, jobseekers.salution, jobseekers.first_name, jobseekers.last_name FROM `applicants`,`jobseekers` WHERE applicants.job_id = ".$this->data['Applicant']['jobid']." AND jobseekers.id = applicants.jobseeker_id");  
                //var_dump($applicants);
                foreach($applicants as $applicant)
                {
                    
                    $temp = $applicant['applicants']['id']; 
                    if($this->data[$temp])
                    {
                        $data['Applicant']['id']=$temp;
                        $data['Applicant']['status']=1;
                        $data['Applicant']['viewed']=0;
                        $reply = $this->data['Applicant']['reply'];
                        $data['Applicant']['reply']=str_replace('[Jobseeker name]',$applicant['jobseekers']['salution']." ".$applicant['jobseekers']['first_name']." ".$applicant['jobseekers']['last_name'],$reply);                        
                        //var_dump($data);
                        $this->Applicant->save($data);
                    }                
                }  
                $this->redirect(array('controller'=>'jobs', 'action'=>'my_jobs'),null, true);  
                
            }            
            
            if(empty($this->data))
            {
                //$job = $this->Applicant->query("SELECT * FROM `jobs` WHERE id =".$id." AND employer_id =".$this->Session->Read('User.id'));

                $job = $this->Applicant->query("SELECT jobs.*,employers.company_name FROM `jobs`,`employers` WHERE jobs.id =".$id." AND employers.id =".$this->Session->Read('User.id')." AND jobs.employer_id =".$this->Session->Read('User.id'));
                //$data = $this->Applicant->Job->find('count',array('conditions'=>array('Job.id'=>$id,'Job.employer_id'=>$this->Session->read('User.id'))));
                if(!empty($job))
                {
                    //$applicants = $this->Applicant->find('all',array('conditions'=>array('Applicant.job_id'=>$id)));
                    /*
                    $query = "SELECT applicants.*, academics.name, cities.name, ";
                    $query .=" jobseekers.date_of_birth, jobseekers.salution, jobseekers.first_name, jobseekers.last_name, jobseekers.city_id,";
                    $query .=" resumes.id, resumes.upload_file, resumes.academic_id, resumes.year_exp FROM `applicants`,`jobseekers`,`resumes`,`academics`,`cities` ";
                    $query .= "WHERE applicants.job_id = ".$id;
                    $query .= " AND jobseekers.id = applicants.jobseeker_id";
                    $query .= " AND resumes.jobseeker_id = jobseekers.id";
                    $query .= " AND resumes.academic_id = academics.id";
                    $query .= " AND jobseekers.city_id = cities.id";
                    */
                     
                    $query = "SELECT applicants.*, academics.name, cities.name, ";
                    $query .=" jobseekers.date_of_birth, jobseekers.salution, jobseekers.first_name, jobseekers.last_name, jobseekers.city_id,";
                    $query .=" resumes.id, resumes.upload_file, resumes.academic_id, resumes.work_exp, resumes.year_exp ";
                    $query .=" FROM `applicants`,`jobseekers` LEFT JOIN `cities` ON ( jobseekers.city_id = cities.id),`resumes` LEFT JOIN `academics` ON ( resumes.academic_id = academics.id ) ";
                    $query .= "WHERE applicants.job_id = ".$id;
                    $query .= " AND jobseekers.id = applicants.jobseeker_id";
                    $query .= " AND resumes.jobseeker_id = jobseekers.id";
                    
                    
                                        
                    
                    
                    $applicants = $this->Applicant->query($query);
                    foreach($applicants as $applicant)
                    {
                        if($applicant['applicants']['status']==0 && $applicant['applicants']['viewed']==0)
                        {
                            $temp['Applicant']['id']=$applicant['applicants']['id'];
                            $temp['Applicant']['viewed']=1;
                            $this->Applicant->save($temp);
                        }
                    }
                    
                    $this->set('job',$job[0]);
                    $this->set('applicants',$applicants);
                }
                else
                {
                    $this->Session->setFlash(__('There is no applicant at this moment', true));
                    $this->redirect(array('controller'=>'jobs', 'action'=>'my_jobs'),null, true); 
                }
                            
            }        
                    
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }
    }

    function resume_alert()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',44);
            $this->Applicant->find('all',array('conditions'=>array()));
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }
    }
    
    function show_cover ( $id = null )
    {
        $this->layout = 'ajax';
        $this->Applicant->recursive=-1;
        $data = $this->Applicant->read(null, $id);
        if($data)
        { 
            $this->set('result',$data['Applicant']['cover']); 
            $this->set('id',$id); 
        } 
        else
        {
            $this->set('result','Invalid ID'); 
            $this->set('id',$id); 
        }
    }
    
    function status ( $id = null )
    {
        $this->layout = 'ajax';
        $this->Applicant->recursive=-1;
        $data = $this->Applicant->read(null, $id);
        if($data)
        { 
            $job = $this->Applicant->query("SELECT * FROM `jobs` WHERE id=".$data['Applicant']['job_id']);
            $jobseeker_id = $data['Applicant']['jobseeker_id'];
            $jobseeker = $this->Applicant->query("SELECT * FROM `jobseekers` WHERE id=".$jobseeker_id);
            $resume = $this->Applicant->query("SELECT * FROM `resumes` WHERE jobseeker_id=".$jobseeker_id);
            $skills = $this->Applicant->query("SELECT * FROM `skills` WHERE jobseeker_id=".$jobseeker_id);
            $languages = $this->Applicant->query("SELECT * FROM `languages` WHERE jobseeker_id=".$jobseeker_id);
            $this->set('job',$job[0]);
            $this->set('jobseeker',$jobseeker[0]);
            $this->set('resume',$resume[0]);
            $this->set('skills',$skills);
            $this->set('languages',$languages);
            $this->set('id',$id);
        } 
        else
        {
            $this->set('result','Invalid ID'); 
            $this->set('id',$id); 
        }
    }
}
?>